package com.meowcook.service.impl;

import com.meowcook.exception.AppException;
import com.meowcook.common.AppResult;
import com.meowcook.common.ResultCode;
import com.meowcook.dao.UserMapper;
import com.meowcook.mapper.UserPojoMapper;
import com.meowcook.pojo.po.User;
import com.meowcook.pojo.vo.UserDetailVO;
import com.meowcook.pojo.vo.UserPartIVO;
import com.meowcook.service.IUserService;
import com.meowcook.utils.MD5Util;
import com.meowcook.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-04-28
 * Time: 15:11
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserPojoMapper userPojoMapper;

    @Override
    public void register(String username, String password) {
        // 校验用户是否存在
        User existUser = userMapper.selectByUsername(username);
        if (existUser != null && existUser.getIsDeleted() == 1) {
            log.warn(ResultCode.FAIL_USER_EXIST.toString());
            throw new AppException(AppResult.fail(ResultCode.FAIL_USER_EXIST));
        }
        // 对密码进行加密
        String salt = UUIDUtil.UUID_32();
        String pwd = MD5Util.md5Salt(password, salt);
        // 封装user
        User user = User.builder(username, pwd, salt);
        // 调用dao
        int row = userMapper.insertSelective(user);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICE.toString());
            throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }

    @Override
    public UserDetailVO login(String username, String password) {
        // 获取用户信息
        User user = userMapper.selectByUsername(username);
        // 判断用户是否存在
        if (user == null || user.getIsDeleted() == 1) {
            log.warn(ResultCode.FAIL_USER_ERROR_LOGIN.toString());
            throw new AppException(AppResult.fail(ResultCode.FAIL_USER_ERROR_LOGIN));
        }
        //校验密码是否正确
        String pwd = MD5Util.md5Salt(password, user.getSalt());
        if (!pwd.equals(user.getPassword())) {
            log.warn(ResultCode.FAIL_USER_ERROR_LOGIN.toString());
            throw new AppException(AppResult.fail(ResultCode.FAIL_USER_ERROR_LOGIN));
        }
        //返回结果
        UserDetailVO result = new UserDetailVO();
        result.setId(user.getId());
        result.setStatus(user.getStatus());
        return result;
    }

    @Override
    public void updateUserLoginStatus(UserDetailVO userDetailVO) {
        // 数据转化
        User user = userPojoMapper.toPO(userDetailVO);
        //调用dao
        int row = userMapper.updateByPrimaryKeySelective(user);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICE.toString());
            throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }

    @Override
    public UserPartIVO getPartUserInfo(Long userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if(user == null || user.getIsDeleted() == 1) {
            throw new AppException(AppResult.fail(ResultCode.FAIL_USER_NOT_EXIST));
        }
        return userPojoMapper.toPartIVO(user);
    }

    @Override
    public UserDetailVO getDetailUserInfo(Long userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (user == null || user.getIsDeleted() == 1) {
            throw new AppException(AppResult.fail(ResultCode.FAIL_USER_NOT_EXIST));
        }
        return userPojoMapper.toDetailVO(user);
    }

    @Override
    public void changePassword(Long userId, String newPassword) {
        User user = userMapper.selectByPrimaryKey(userId);
        String salt = user.getSalt();
        String pwd = MD5Util.md5Salt(newPassword, salt);
        user = new User();
        user.setId(userId);
        user.setPassword(pwd);
        int row = userMapper.updateByPrimaryKeySelective(user);
        if (row != 1) {
            throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }

    @Override
    public void changeNickname(Long UserId, String newNickname) {
        User user = new User();
        user.setId(UserId);
        user.setNickname(newNickname);
        int row = userMapper.updateByPrimaryKeySelective(user);
        if (row != 1) {
            throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }
}
